<?php

class Db
{
 private $queries = array();
 private $times = array();
 private $params;

 static private $_instance = NULL;

 private function __construct()
 {
     $this->params = Registry::getInstance();
     $this->connect();
 }
 
 static public function getInstance()
 {
     if (self::$_instance == NULL)
     {
         self::$_instance = new Db();
     }
     return self::$_instance;
 }


 private function connect()
 {
     @mysql_connect($this->params['db']['server'], $this->params['db']['user'], $this->params['db']['pass']) or die("<b>Error: </b><br /><pre>".mysql_error()."</pre>");
     @mysql_select_db($this->params['db']['db_name']) or die("<b>Error : </b><br /><pre>".mysql_error()."</pre>");
     @mysql_query("SET CHARACTER SET 'utf8'");
 }
 
 public function q($query)
 {
    $st = $this->microtime_float();
    $r = @mysql_query($query) or die("<b>Error : </b><br />$query<br /><pre>".mysql_error()."</pre>");
    $fn = $this->microtime_float();
    $this->queries[] = $query;
    $this->times[] = $fn - $st;
    return $r;
 }
 
 public function qri ($query)
 {
        $r = $this->q($query);
        if (mysql_num_rows($r) > 0) 
        {
            $res = array();
            if (mysql_num_rows($r) == 1) 
            {
                $arr = mysql_fetch_row($r);
                array_push($res, $arr);
                return $res;
            } 
            else 
            {
                
                 while ($arr = mysql_fetch_row($r)) 
                 {
                    array_push($res, $arr);
                 }
                return $res;
            }
        } 
        else 
       {
            return NULL;
       }
 }

 public function qr($query,$assoc = 0) 
 {
     
        $r = $this->q($query);
        if (mysql_num_rows($r) > 0) 
        {
            $res = array();
            if (mysql_num_rows($r) == 1) 
            {
                $arr = ($assoc==1)?mysql_fetch_assoc($r):mysql_fetch_array($r);
                array_push($res, $arr);
                return $res;
            } 
            else 
            {
                
                 while ($arr = ($assoc==1)?mysql_fetch_assoc($r):mysql_fetch_array($r)) 
                 {
                    array_push($res, $arr);
                 }
                return $res;
            }
        } 
        else 
       {
            return NULL;
       }
  }


 public function nr($query)
 {
    return mysql_num_rows($this->q($query));
 }

 //last id
 public function lid()
 {
    return mysql_insert_id();
 }

 // fermeture de la connect mysql
 public function c()
 {
    mysql_close();
 }
 
 public function debug()
 {
     $i = 0;
     echo '<h2>MySQL debug</h2>
<p>Total Queries: <span style="font-size: 24px">'.count($this->queries).'</span></p>         
<ul>';
     
     foreach ($this->queries as $qr) 
     {
         echo '<li>'.$i.' <b>('.$qr.')</b>   Время выполнения:'.$this->times[$i].'сек.</li>';
         $i++;
     }
     echo '</ul>';
 }
 
 public function table_exists ($table)
 {
     if (mysql_num_rows($this->q("show tables like '".$table."'")))
     {
         return true;
     }
     
     return false;
 }


 private function microtime_float()
 {
     list($usec, $sec) = explode(" ", microtime());
     return ((float)$usec + (float)$sec);
 }



}

?>
